import processing.serial.*;

int iWidth = 50;

int iNumLeds = 11;
int iCurrLed = 0;

int iNumScenes = 6;
int iCurrScene = 0;

int yOffset = 100;

byte[] bValues = new byte[8];
boolean bSendData = true;
Serial sPort;

HScrollbar[] hs = new HScrollbar[3];
HButton[] hb = new HButton[iNumLeds];

int [][][] iColors = new int[iNumScenes][iNumLeds][3];

void setup(){

  size(iWidth*iNumLeds, iWidth+300, P2D);
  frameRate(25);
  rectMode(CORNER);
  colorMode(RGB, 1024);

  textFont(createFont("Calibri",32 ,true));
  textMode(SCREEN);
  textAlign(LEFT, TOP);   

  for (int f=0; f<3; f++){
    hs[f] = new HScrollbar(30, iWidth + yOffset + 13 + 40*f, iWidth*iNumLeds - 50, 6);
  }

  for (int f=0; f<iNumScenes; f++){
    for (int g=0; g<iNumLeds; g++){
      iColors[f][g][0]=iColors[f][g][1]=iColors[f][g][2]=0;
    }  
  }
   	
  String rgbValues[] = loadStrings("data.txt");
  for (int f=0; f<iNumScenes; f++){
    for (int g=0; g<iNumLeds; g++){
      for (int h=0; h<3; h++){
        iColors[f][g][h]=int(rgbValues[f*iNumLeds*3+g*3+h]);
        //println (f*iNumLeds*3+g*3+h);
      }
    }  
  }  

  for (int f=0; f<iNumLeds; f++){
    hb[f] = new HButton(f*iWidth, 0, iWidth, iWidth*2);
    hb[f].setColor(iColors[iCurrScene][f][0],iColors[iCurrScene][f][1],iColors[iCurrScene][f][2]);
  }

  buttonToBar();

  println(Serial.list());
  String portName = Serial.list()[0];
  sPort = new Serial(this, portName, 57600);  

  bValues[0]=127;

}


void draw() {

  background(0);

  for (int f=0; f<3; f++){
    hs[f].update();
    hs[f].display();    
  }  

  int r = hs[0].getPosScaled(1023);
  int g = hs[1].getPosScaled(1023);
  int b = hs[2].getPosScaled(511);  

  bSendData = hb[iCurrLed].setColor(r,g,b);

  for (int f=0; f<iNumLeds; f++){
    hb[f].display();    
  }  
  fill(1000, 200, 200);
  rect(hb[iCurrLed].xpos, hb[iCurrLed].ypos+hb[iCurrLed].sheight, hb[iCurrLed].swidth, 8);

  fill(1023);     
  text("R",3,iWidth+yOffset);    
  text("G",3,iWidth+yOffset+40);    
  text("B",3,iWidth+yOffset+80);   
  text (r + ","+ g + ","+ b, 30, iWidth+yOffset+130);

  text ("Scene "+ (iCurrScene+1) + "/"+ iNumScenes, width/2, iWidth+yOffset+130);

  if (bSendData) {
    iColors[iCurrScene][iCurrLed][0] = r;
    iColors[iCurrScene][iCurrLed][1] = g;
    iColors[iCurrScene][iCurrLed][2] = b;
    writeData(r,g,b);
  }

}


void writeData(int r, int g, int b){
  writeData(r,g,b,iCurrLed);
}

void writeData(int r, int g, int b, int l){
  bValues[1] = (byte)l;
  bValues[2] = (byte)((r & 0x3E0) >> 5);
  bValues[3] = (byte)((r & 0x1F));  
  bValues[4] = (byte)((g & 0x3E0) >> 5);
  bValues[5] = (byte)((g & 0x1F));    
  bValues[6] = (byte)((b & 0x3E0) >> 5);
  bValues[7] = (byte)((b & 0x1F));   
  sPort.write(bValues);  
}


void buttonToBar(){
  hs[0].setPosScaled(hb[iCurrLed].getR()/1023.0);
  hs[1].setPosScaled(hb[iCurrLed].getG()/1023.0);
  hs[2].setPosScaled(hb[iCurrLed].getB()/511.0);  
}


void keyPressed() {

  boolean bChangeValue = false;

  if (key==CODED && keyCode==DOWN) {
    if (++iCurrScene>=iNumScenes) iCurrScene = 0;
    bChangeValue = true;
  }

  if (key==CODED && keyCode==UP) {
    if (--iCurrScene<0) iCurrScene = iNumScenes-1;
    bChangeValue = true;
  }  

  if (key==CODED && keyCode==RIGHT) {
    if (++iCurrLed>=iNumLeds) iCurrLed = 0;
    bChangeValue = true;    
  }

  if (key==CODED && keyCode==LEFT) {
    if (--iCurrLed<0) iCurrLed = iNumLeds-1;
    bChangeValue = true;
  }  
  
  if (key=='g' || key=='G'){
    saveData();
  }

  if (bChangeValue) {
    for (int f=0; f<iNumLeds; f++){
      hb[f].setColor(iColors[iCurrScene][f][0],iColors[iCurrScene][f][1],iColors[iCurrScene][f][2]);
      writeData(iColors[iCurrScene][f][0],iColors[iCurrScene][f][1],iColors[iCurrScene][f][2],f);
    }    
    buttonToBar();
  }

}

void saveData(){

  String[] sOut = new String[1];
  
  sOut[0] = "int iKitColores[totalKits][totalLeds][3] = {";
  for (int f=0; f<iNumScenes; f++){
    sOut[0] += "{";
    for (int g=iNumLeds-1; g>=0; g--){
      sOut[0] += "{"+iColors[f][g][0]+","+iColors[f][g][1]+","+iColors[f][g][2]+"}";
      sOut[0] += (g>0?",":"");
    }  
    sOut[0] += "}";    
    sOut[0] += (f<iNumScenes-1?",\n":"");    
  }
  sOut[0] += "};";
  saveStrings("colores.txt",  sOut);
  
  sOut[0] = "";
  for (int f=0; f<iNumScenes; f++){
    for (int g=0; g<iNumLeds; g++){
      sOut[0] += iColors[f][g][0]+"\n"+iColors[f][g][1]+"\n"+iColors[f][g][2]+"\n";
    }  
  }
  saveStrings("data.txt",sOut);

}

void stop(){
  super.stop();
}







